package com.geping.etl.common.security.authentication.impl;


import com.geping.etl.common.entity.Sys_Auth_Role_Resource;
import com.geping.etl.common.security.authentication.AuthOrder;
import com.geping.etl.common.security.authentication.AuthOrderConstant;
import com.geping.etl.common.security.authentication.AuthServiceContainer;
import com.geping.etl.common.security.authentication.BasicAuthService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class MenuAuthServiceImpl  extends AuthServiceContainer implements BasicAuthService, AuthOrder, Comparable<AuthOrder> {

    /**
     * 判断是否有菜单权限
     * @return
     */
    @Override
    public boolean auth(String url, HttpServletRequest request) {
        List<Sys_Auth_Role_Resource> resourceList = (List<Sys_Auth_Role_Resource>)request.getSession().getAttribute("resources");
        return hasUrl(resourceList,url);
    }

    private  boolean hasUrl(List<Sys_Auth_Role_Resource> resourceList, String url){
        for(Sys_Auth_Role_Resource sysAuthRoleResource:resourceList){
            if(StringUtils.isNotBlank(sysAuthRoleResource.getResType())&&sysAuthRoleResource.getResType().equals("Menu")){
                String authUrl=sysAuthRoleResource.getResValue();
                if(authUrl.equals(url)){
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    public int getOrder() {
        return AuthOrderConstant.menuAuthService;
    }

    @Override
    public int compareTo(AuthOrder o) {
        return this.getOrder()-o.getOrder();
    }
}